import { createDiscreteApi } from "naive-ui";

export default defineNuxtRouteMiddleware((to, from) => {
    // 获取Cookie实例    
    const token = useCookie("token");

    // 获取用户实例
    const user = useUser();

    // 获取Nuxt实例
    const nuxt = useNuxtApp();

    // 未登录
    if (!token.value) {
        if (import.meta.client) {
            const { message } = createDiscreteApi(["message"])
            message.error("请先登录")
        }
        return navigateTo("/login?from=" + nuxt._route.fullPath)
    }


    // 未绑定手机号
    const phone = user.value?.phone
    if (!phone && nuxt._route.name != 'bindphone') {
        if (import.meta.client) {
            const { message } = createDiscreteApi(["message"])
            message.error("请先绑定手机号")
        }
        return navigateTo("/bindphone?from=" + nuxt._route.fullPath)
    }

})